package com.iflytek.autofly.setting.biz.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.iflytek.autofly.setting.R;
import com.iflytek.sunflower.FlowerCollector;

/**
 * <b>Description: </b>用于编辑的弹出窗口<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/11/18</td>
 * <td>16:54</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class EditActivity extends Activity {
    public static final String NAME = "name";
    public static final String ORI_VALUE = "ori_value";
    public static final String HINT_VALUE = "hint_value";
    public static final String INPUT_TYPE = "input_type";
    public static final String RESULT = "result";
    protected EditText mEditEt;
    protected Button mSaveBtn;
    protected CheckBox mShowPassword;
    private String mOriValue;
    private String mHintValue;
    private int mInputType;
    private int mExpandResId = -1;

    public void addExpandView(int expandResId) {
        mExpandResId = expandResId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_window);

        Intent intent = this.getIntent();
        final Bundle bundle = intent.getExtras();
        String name = bundle.getString(NAME, "");
        mOriValue = bundle.getString(ORI_VALUE, "");
        mHintValue = bundle.getString(HINT_VALUE, "");
        mInputType = bundle.getInt(INPUT_TYPE, -1);

        notifySystem(name);

        if (mExpandResId != -1) {
            LinearLayout expandLl = (LinearLayout) findViewById(R.id.edit_window_expand_ll);
            expandLl.addView(getLayoutInflater().inflate(mExpandResId, null));
        }

        mEditEt = (EditText) findViewById(R.id.edit_window_edit_et);
        if (!TextUtils.isEmpty(mOriValue)) {
            mEditEt.setText(mOriValue);
            mEditEt.setSelection(0, mOriValue.length());
        }
        if (!TextUtils.isEmpty(mHintValue)) {
            mEditEt.setHint(mHintValue);
        }
        if (mInputType != -1) {
            mEditEt.setInputType(mInputType);
        }

        mSaveBtn = (Button) findViewById(R.id.edit_window_save_btn);
        mSaveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                String result = mEditEt.getText().toString();
                if (!mOriValue.equals(result)) {
                    bundle.putString(EditActivity.RESULT, mEditEt.getText().toString());
                    expandBundle(bundle);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                }
                finish();
            }
        });

        mShowPassword = (CheckBox) findViewById(R.id.show_password);
        mShowPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                int pos = mEditEt.getSelectionEnd();
                mEditEt.setInputType(
                        InputType.TYPE_CLASS_TEXT | (isChecked ?
                                InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
                                InputType.TYPE_TEXT_VARIATION_PASSWORD));
                if (pos >= 0) {
                    mEditEt.setSelection(pos);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        mExpandResId = -1;
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        FlowerCollector.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        FlowerCollector.onPause(this);
    }

    protected void expandBundle(Bundle bundle) {

    }

    private void notifySystem(String name) {
        Intent intnet = new Intent("com.iflytek.car.USER_ACTION");
        intnet.putExtra("name", name);
        sendBroadcast(intnet);
    }
}
